from ihome import db
from datetime import datetime
from werkzeug.security import generate_password_hash


class BaseModel():
    '''模型基类'''
    create_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime, default=datetime.now,
                            onupdate=datetime.now)


class User(BaseModel, db.Model):
    '''用户表'''
    __tablename__ = 'ih_user'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(32), unique=True, nullable=False)
    mobile = db.Column(db.String(11), unique=True, nullable=False)
    password_hash = db.Column(db.String(128), nullable=False)
    avatar_url = db.Column(db.String(128))
    real_name = db.Column(db.String(32))
    real_id_card = db.Column(db.String(18))
    house = db.relationship('House', backref='user') # 建立关联关系，方便查找
    order = db.relationship('Order', backref='user')

    # property装饰器，会把password函数变为属性
    @property
    def password(self):
        '''函数的返回值作为属性值返回
        读取属性时被调用user.password
        '''
        raise AttributeError('该属性是只能设置，不能读取')

    # 使用setter对password属性进行设置操作,不需要有返回值，设置属性时被调用
    @password.setter
    def password(self, value):
        self.password_hash = generate_password_hash(value)


class Area(BaseModel, db.Model):
    '''区域表'''

    __tablename__ = 'ih_area'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(32), unique=True, nullable=False)
    house = db.relationship("House", backref="area")


# 房屋设备表,设备与房屋建立多对多关系,sqlalchemy对象，联合主键
house_facility = db.Table(
    'ih_house_facility',
    db.Column("house_id", db.Integer, db.ForeignKey("ih_house.id"), primary_key=True),
    db.Column("facility_id", db.Integer, db.ForeignKey("ih_facility.id"), primary_key=True)
)


class House(BaseModel, db.Model):
    '''房间表'''

    __tablename__ = 'ih_house'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('ih_user.id'), nullable=False)
    area_id = db.Column(db.Integer, db.ForeignKey('ih_area.id'), nullable=False)
    title = db.Column(db.String(128), nullable=False)
    price = db.Column(db.Integer, default=0)  # 单价，单位：分
    address = db.Column(db.String(512), default="")  # 地址
    room_count = db.Column(db.Integer, default=1)  # 房间数目
    acreage = db.Column(db.Integer, default=0)  # 房屋面积
    unit = db.Column(db.String(32), default="")  # 房屋单元， 如几室几厅
    capacity = db.Column(db.Integer, default=1)  # 房屋容纳的人数
    beds = db.Column(db.String(64), default="")  # 房屋床铺的配置
    deposit = db.Column(db.Integer, default=0)  # 房屋押金
    min_days = db.Column(db.Integer, default=1)  # 最少入住天数
    max_days = db.Column(db.Integer, default=0)  # 最多入住天数，0表示不限制
    order_count = db.Column(db.Integer, default=0)  # 预订完成的该房屋的订单数
    index_image_url = db.Column(db.String(256), default="")  # 房屋主图片的路径
    images = db.relationship('HouseImage') # 房屋图片
    orders = db.relationship('Orders', backref='house') # 房屋的订单

    facility = db.relationship("Facility", secondary=house_facility) # secondary设置中间表

class HouseImage(BaseModel, db.Model):
    '''房屋图片表'''

    __tablename__ = 'ih_house_image'

    id = db.Column(db.Integer, primary_key=True)
    url = db.Column(db.String(128), nullable=False)
    house_id = db.Column(db.Integer, db.ForeignKey("ih_house.id"), nullable=False)


class Facility(BaseModel, db.Model):
    """设备表"""

    __tablename__ = 'ih_facility'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(32), nullable=False, unique=True)

'''
class HouseFacility(BaseModel, db.Model):
    """房屋设备表"""

    __tablename__ = 'ih_house_facility'

    id = db.Column(db.Integer, primary=True)
    house_id = db.Column(db.Integer, db.ForeignKey("ih_house.id"), nullable=False)
    facility_id = db.Column(db.Integer, db.ForeignKey("ih_facility.id"), nullable=False)
'''



class Orders(BaseModel, db.Model):
    '''订单表'''
    __tablename__ = 'ih_orders'

    id = db.Column(db.Integer, primary_key=True)  # 订单编号
    user_id = db.Column(db.Integer, db.ForeignKey("ih_user.id"), nullable=False)
    hosue_id = db.Column(db.Integer, db.ForeignKey("ih_house.id"), nullable=False)
    start_time = db.Column(db.DateTime, nullable=False)
    end_time = db.Column(db.DateTime, nullable=False)
    days = db.Column(db.Integer, nullable=False) # 预订的总天数
    house_price = db.Column(db.Integer, nullable=False) # 房屋的单价
    amount = db.Column(db.Integer, nullable=False) # 订单的总金额
    status = db.Column(
        db.Enum(
            "WAIT_ACCEPT",  # 待接单,
            "WAIT_PAYMENT",  # 待支付
            "PAID",  # 已支付
            "WAIT_COMMENT",  # 待评价
            "COMPLETE",  # 已完成
            "CANCELED",  # 已取消
            "REJECTED"  # 已拒单
        ),
        default='WAIT_ACCEPT', index=True
    )
    comment = db.Column(db.Text)

